package cate.game.rank.h;

import cate.common.table.d.GDRank;
import cate.game.rank.po.Rank;
import cate.game.rank.po.RankSPO;
import cate.game.role.Role;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class RankPowerHandler extends RankHandler<RankSPO<Rank>, Rank> {
    @Override
    protected String getTag() {
        return "power";
    }

    @Override
    protected RankSPO<Rank> createSPO() {
        return new RankSPO<>();
    }

    @Override
    public int getRankId() {
        return GDRank.Id.POWER_RANK_ID;
    }

    @Override
    public void record(Role role, long newValue) {
        Rank rank = getByUid(role.getUid());
        if(rank == null || newValue > rank.value){
            super.record(role, newValue);
        }
    }

    public synchronized boolean inTop10(String roleUid){
        final List<Rank> ranks = getRankList(null);
        for(int i=0; i<Math.min(10, ranks.size()); ++i){
            if(StringUtils.equals(ranks.get(i).owner.uid, roleUid)){
                return true;
            }
        }
        return false;
    }
}
